package com.haierp.util.yitao;

import com.haierp.util.DimensionCodeUtil;
import com.haierp.util.HttpClientUtil;
import com.haierp.util.Md5Util;
import com.haierp.util.PropertiesUtils;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class YtDimensionCodeUtil {
    //stg环境
    //private static String HttpUrl = "http://47.112.139.251:8888/";
    //本地
    //private static String HttpUrl = "http://192.168.1.12:8889/";
    //生产
    private static String HttpUrl = "http://47.112.7.183:8888/";

    public static String yitaoHttpRequest(String path, Map<String, String> params, String method) throws Exception {
        String appKey = PropertiesUtils.getProperties().getProperty("yitao.appKey");
        String appSecret = PropertiesUtils.getProperties().getProperty("yitao.appSecret");
        Integer timestamp = DimensionCodeUtil.getSecondTimestamp(new Date());
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("timestamp", timestamp.toString());
        paramMap.put("app-key", appKey);
        if ("GET".equals(method)) {
            paramMap.putAll(params);
        }
        String pathString = path + "?" + DimensionCodeUtil.getParamsSort(paramMap);
        String md5 = Md5Util.getMD5(pathString + appSecret);
        Map<String, String> header = new HashMap<>();
        header.put("timestamp", timestamp.toString());
        header.put("app-key", appKey);
        header.put("sign", md5);
        Map<String, Object> param = new HashMap<>();
        param.putAll(params);
        String string = null;
        string = HttpClientUtil.post(HttpUrl + path, header, param, "2", "2");
        return string;
    }

    /**
     * 发送HTTPpost请求
     * @param path
     * @param params
     * @param method
     * @return
     * @throws Exception
     */
    public static String yitaoHttpPostRequest(String path, Map<String, Object> params, String method) throws Exception {
        String appKey = PropertiesUtils.getProperties().getProperty("yitao.appKey");
        String appSecret = PropertiesUtils.getProperties().getProperty("yitao.appSecret");
        Integer timestamp = DimensionCodeUtil.getSecondTimestamp(new Date());
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("timestamp", timestamp.toString());
        paramMap.put("app-key", appKey);
        String pathString = path + "?" + DimensionCodeUtil.getParamsSort(paramMap);
        String md5 = Md5Util.getMD5(pathString + appSecret);
        Map<String, String> header = new HashMap<>();
        header.put("timestamp", timestamp.toString());
        header.put("app-key", appKey);
        header.put("sign", md5);
        Map<String, Object> param = new HashMap<>();
        param.putAll(params);
        String string = null;
        string = HttpClientUtil.post(HttpUrl + path, header, param, "2", "2");
        return string;
    }

    /**
     * post请求
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url,JSONObject json){

        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(HttpUrl +url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString(), Charset.forName("UTF-8"));
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json;charset=utf-8");//发送json数据需要设置contentType
            post.addHeader("Content-type","application/json; charset=utf-8");
            post.setHeader("Accept", "application/json");
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                String result = EntityUtils.toString(res.getEntity());// 返回json格式：
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }


}
